package day05;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet("*.cart")
public class CartServlet extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();

        if ("/add.cart".equals(path)) {
            add(request,response);
        }

        if ("/clear.cart".equals(path)) {
            clear(request,response);
        }

        if ("/removeByProductId.cart".equals(path)) {
            removeByProductId(request,response);
        }

        if ("/removeByProductIds.cart".equals(path)) {
            removeByProductIds(request,response);
        }

        if ("/modify.cart".equals(path)) {
            modify(request,response);
        }

    }
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("CartServlet.add()");

        Integer productId=Integer.parseInt(request.getParameter("productId"));

        Cart cart= (Cart) request.getSession().getAttribute("cart");

        cart.add(productId);

//        request.getSession().setAttribute("cart",cart);

        response.sendRedirect(request.getContextPath()+"/list.product");
    }

    protected void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("CartServlet.clear()");

        Cart cart= (Cart) request.getSession().getAttribute("cart");

        cart.clear();

        response.sendRedirect(request.getContextPath()+"/shop/cart.jsp");
    }

    protected void removeByProductId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("CartServlet.removeByProductId()");

        Integer productId=Integer.parseInt(request.getParameter("productId"));

        Cart cart= (Cart) request.getSession().getAttribute("cart");

        cart.removeByProductId(productId);

        response.sendRedirect(request.getContextPath()+"/shop/cart.jsp");
    }

    protected void removeByProductIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("CartServlet.removeByProductIds()");

        List<Integer> productIds= Arrays.stream(request.getParameterValues("productIds"))
                .map(Integer::parseInt)
                .collect(Collectors.toList());

        Cart cart= (Cart) request.getSession().getAttribute("cart");

        cart.removeByProductIds(productIds);

        response.sendRedirect(request.getContextPath()+"/shop/cart.jsp");

    }

    protected void modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("CartServlet.modify()");

        Integer productId=Integer.parseInt(request.getParameter("productId"));
        Integer num=Integer.parseInt(request.getParameter("num"));

        Cart cart= (Cart) request.getSession().getAttribute("cart");

        cart.modify(productId,num);

        response.sendRedirect(request.getContextPath()+"/shop/cart.jsp");
    }



}
